package com.gxa.dr2.controller;

import com.gxa.dr2.dto.Response;
import com.gxa.dr2.dto.ResultDTO;
import com.gxa.dr2.pojo.Empl;
import com.gxa.dr2.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 登录页面
     * @return
     */
    @GetMapping("/page")
    public String page(){
        return "login";
    }


    /**
     * 登录操作
     * @return
     */
    @PostMapping("/do")
    @ResponseBody
    public ResultDTO loginDo(Empl empl, HttpSession session){
        return loginService.login(empl, session);
    }


    /**
     * 登出操作
     * @return
     */
    @PostMapping("/out")
    @ResponseBody
    public ResultDTO logOut(HttpSession session){
        // 清空session
        session.removeAttribute("emplName");
        return Response.success();
    }
}
